package com.fishingwithme.infrastructure.cfg.types;

import cn.fishingwithme.domain.spots.*;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fishingwithme.domain.spots.*;

import java.io.IOException;

public class Deserializers {

    public static class ScoreTypeDeserializer extends JsonDeserializer<ScoreType> {
        @Override
        public ScoreType deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
            String value = p.getText();
            try {
                // 尝试通过枚举名称反序列化
                return ScoreType.valueOf(value);
            } catch (IllegalArgumentException e) {
                // 尝试通过type值反序列化
                try {
                    int type = Integer.parseInt(value);
                    return ScoreType.of(type);
                } catch (IllegalArgumentException ex) {
                    throw new IOException("无法反序列化收获类型: " + value, ex);
                }
            }
        }
    }

    public static class HarvestTypeDeserializer extends JsonDeserializer<HarvestType> {
        @Override
        public HarvestType deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
            String value = p.getText();
            try {
                // 尝试通过枚举名称反序列化
                return HarvestType.valueOf(value);
            } catch (IllegalArgumentException e) {
                // 尝试通过type值反序列化
                try {
                    int type = Integer.parseInt(value);
                    return HarvestType.of(type);
                } catch (IllegalArgumentException ex) {
                    throw new IOException("无法反序列化收获类型: " + value, ex);
                }
            }
        }
    }

    public static class SpotTypeDeserializer extends JsonDeserializer<SpotType> {
        @Override
        public SpotType deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
            String value = p.getText();
            try {
                // 尝试通过枚举名称反序列化
                return SpotType.valueOf(value);
            } catch (IllegalArgumentException e) {
                // 尝试通过type值反序列化
                try {
                    int type = Integer.parseInt(value);
                    return SpotType.of(type);
                } catch (IllegalArgumentException ex) {
                    throw new IOException("无法反序列化收获类型: " + value, ex);
                }
            }
        }
    }

    public static class WaterTypeDeserializer extends JsonDeserializer<WaterType> {
        @Override
        public WaterType deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
            String value = p.getText();
            try {
                // 尝试通过枚举名称反序列化
                return WaterType.valueOf(value);
            } catch (IllegalArgumentException e) {
                // 尝试通过type值反序列化
                try {
                    int type = Integer.parseInt(value);
                    return WaterType.of(type);
                } catch (IllegalArgumentException ex) {
                    throw new IOException("无法反序列化收获类型: " + value, ex);
                }
            }
        }
    }

    public static class FishTypeDeserializer extends JsonDeserializer<FishType> {
        @Override
        public FishType deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
            String value = p.getText();
            try {
                // 尝试通过枚举名称反序列化
                return FishType.valueOf(value);
            } catch (IllegalArgumentException e) {
                // 尝试通过type值反序列化
                try {
                    int type = Integer.parseInt(value);
                    return FishType.of(type);
                } catch (IllegalArgumentException ex) {
                    throw new IOException("无法反序列化收获类型: " + value, ex);
                }
            }
        }
    }

    public static class ParkingDistanceDeserializer extends JsonDeserializer<ParkingDistance> {
        @Override
        public ParkingDistance deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
            String value = p.getText();
            try {
                // 尝试通过枚举名称反序列化
                return ParkingDistance.valueOf(value);
            } catch (IllegalArgumentException e) {
                // 尝试通过type值反序列化
                try {
                    int type = Integer.parseInt(value);
                    return ParkingDistance.of(type);
                } catch (IllegalArgumentException ex) {
                    throw new IOException("无法反序列化收获类型: " + value, ex);
                }
            }
        }
    }

    public static class SpotLvlDeserializer extends JsonDeserializer<SpotLvl> {
        @Override
        public SpotLvl deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
            String value = p.getText();
            try {
                // 尝试通过枚举名称反序列化
                return SpotLvl.valueOf(value);
            } catch (IllegalArgumentException e) {
                // 尝试通过type值反序列化
                try {
                    int type = Integer.parseInt(value);
                    return SpotLvl.of(type);
                } catch (IllegalArgumentException ex) {
                    throw new IOException("无法反序列化收获类型: " + value, ex);
                }
            }
        }
    }
}
